package com.telecom.at;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.telecom.at.db.model.User;

@WebFilter(filterName = "ValidateFilter", 
urlPatterns = { "/man/*", "/front/*" })
public class ValidateFilter implements Filter {
	public void init(FilterConfig fConfig) throws ServletException {
	}
	public void destroy() {
	}
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		HttpServletResponse httpServletResponse = (HttpServletResponse) response;

		HttpSession session = httpServletRequest.getSession();
		User curUser = (User) session.getAttribute("curUser");
		//System.out.println(curUser);
		String manPrefix = httpServletRequest.getContextPath() + "/man/";
		//System.out.println(manPrefix);
		String frontPrefix = httpServletRequest.getContextPath() + "/front/";
		//System.out.println(frontPrefix);
		String requestURI = httpServletRequest.getRequestURI();
		//System.out.println(requestURI);

		if (curUser == null) {
			httpServletResponse.sendRedirect(request.getServletContext().getContextPath() + "/Login");
		} else  {
			chain.doFilter(request, response);
		}		
		
		//chain.doFilter(request, response);
	}
}